import 'dart:ui';
import 'package:flutter/material.dart';

List<String> getPageStringOffsetList(
  String textContent,
  double width,
  double height,
  TextStyle textStyle,
) {
  if (textContent == null) {
    return [""];
  }
  var textPainter = TextPainter(textDirection: TextDirection.ltr);
  textPainter.text = TextSpan(text: textContent, style: textStyle);
  textPainter.layout(maxWidth: width);
  double oneLineHeight = textPainter.computeLineMetrics()[0].height;
  List<LineMetrics> lineMetrics = textPainter.computeLineMetrics();
  int onePageLineCount = height ~/ oneLineHeight;

  List<String> allPageList = [];
  int i = 1;
  String tempPageString = '';
  lineMetrics.forEach((m) {
    // 提取出一段文本中每行的方法
    var startPosition = textPainter.getPositionForOffset(Offset(m.left, m.baseline));
    var endPosition = textPainter.getPositionForOffset(Offset(m.left + m.width, m.baseline));
    var substr = textContent.substring(startPosition.offset, endPosition.offset);
    // print('line: ${m.lineNumber} => ${startPosition.offset} [$substr] ${endPosition.offset}');
    tempPageString += !m.hardBreak ? substr: (substr + '\n');
    if(i % onePageLineCount == 0 || i == lineMetrics.length){
        allPageList.add(tempPageString);
        tempPageString = '';
    }
    i ++;
});
  return allPageList;
}

int currentTimeMillis() {
    return new DateTime.now().millisecondsSinceEpoch;
}